package com.example.form.service.impl;

import com.example.form.common.AppResult;
import com.example.form.common.ResultCode;
import com.example.form.dao.MessageMapper;
import com.example.form.exception.ApplicationException;
import com.example.form.model.Message;
import com.example.form.model.User;
import com.example.form.service.IMessageService;
import com.example.form.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServicelmpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;//注入站内信表的操作1类
    @Resource
    private IUserService userService;//注入操作用户表的实现类
    @Override
    public void create(Message message) {
        //非空校验
        if(message==null||message.getPostUserId()==null||message.getReceiveUserId()==null){//站内信为null或者发帖或接收者的id为null
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());//参数校验失败异常
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接收者是否存在
        User user=userService.selectById(message.getReceiveUserId());//通过站内信里面的接受者id查询
        if (user==null){
            //抛出异常
            throw new ApplicationException(AppResult.failed("回复用户不存在"));
        }
        //设置默认值(传入的message是不全的)
        message.setState((byte)0);//表示未读状态
        message.setDeleteState((byte)0);
        //设置创建与更新时间
        Date date =new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //调用dao
        int row=messageMapper.insertSelective(message);//动态插入站内信到数据库中
        //判断执行
        if (row!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //非空校验
        if(receiveUserId==null||receiveUserId<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //直接调用dao
        Integer count=messageMapper.selectUnreadCount(receiveUserId);//根据接收者的id来查询其站内信的数量
        //返回结果
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //非空校验
        if (receiveUserId==null){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao
        List<Message> messages=messageMapper.selectByReceiveUserId(receiveUserId);
        //返回结果
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //非空校验 state:0未读  1已读  2已回复
        if (id==null||id<0|| state<0||state>2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构造要更新的对象
        Message updateMessage=new Message();
        updateMessage.setId(id);//站内信id
        updateMessage.setState(state);//要更新的状态
        Date date=new Date();
        updateMessage.setCreateTime(date);//更改时间
        //调用dao
        int row=messageMapper.updateByPrimaryKeySelective(updateMessage);//动态修改(根据站内信的id来查找的)
        //判断执行结果
        if(row!=1){
            //打印日志
            log.warn(ResultCode.FAILED.toString());//操作失败
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {
        //非空校验
        if (id==null||id<0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao
        Message message=messageMapper.selectByPrimaryKey(id);
        //返回
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {//message对象是包括发送者id接收者id以及回复的内容的
        //非空校验
        if(repliedId==null||repliedId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验repliedId(登录用户回复的站内信的id)对应的站内信的状态
        Message existsMessage=messageMapper.selectByPrimaryKey(repliedId);//通过站内信id查询到其对应的信息
        if (existsMessage==null||existsMessage.getDeleteState()==1){
            //不存在或者已删除
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //更新要回复的帖子的state状态为2(已回复)
        updateStateById(repliedId,(byte)2);//调用上面写的更新指定站内信状态的方法
        //插入新的站内信
        create(message);//调用上面的发送(创建)站内信的方法
    }
}
